package com.bto.solar.work.controller;

import com.alibaba.fastjson2.JSONObject;
import com.bto.solar.work.common.R;
import com.bto.solar.work.constant.BizStageEnum;
import com.bto.solar.work.constant.BizTaskEnum;
import com.bto.solar.work.constant.Constants;
import com.bto.solar.work.constant.DBConstants;
import com.bto.solar.work.dto.ApprovalReq;
import com.bto.solar.work.dto.lease.FileAndContactAsyncPushRes;
import com.bto.solar.work.dto.lease.LeaseApprovalNotifyReq;
import com.bto.solar.work.dto.lease.LeaseApprovaledNotifyResp;
import com.bto.solar.work.dto.lease.OperatingStatusQueryRes;
import com.bto.solar.work.lease.LeaseApi;
import com.bto.solar.work.service.BtoDesignService;
import com.bto.solar.work.service.BtoOrderStateService;
import io.swagger.annotations.ApiModelProperty;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author 王小波
 * @description:   审批接口
 * @date 2023/8/10 16:26
 */
@RestController
@Slf4j
@RequestMapping("/approval")
public class ApprovalController {
    @Autowired
    private LeaseApi leaseApi;
    @Autowired
    private BtoOrderStateService btoOrderStateService;
    @Autowired
    private BtoDesignService btoDesignService;

    @ApiModelProperty("提交初审-预审信息")
    @PostMapping("/put-approval/lease/pre-approval")
    public R<String> putPreApproval(@RequestParam String orderId){
        String stateId = btoOrderStateService.updateToWaitingApprovalState(orderId , BizStageEnum.PRE_APPROVAL.getStageId(),
                BizTaskEnum.TASK_YSXX.getTaskId());
        return R.ok(stateId);
    }

    @ApiModelProperty("提交-审批-客户信息")
    @PostMapping("/put-approval/bto/customer")
    public R doPreCustomer(@RequestParam String orderId){
        String stateId = btoOrderStateService.updateToWaitingApprovalState(orderId , BizStageEnum.CUSTOMER_INFO.getStageId(),
                null);
        return R.ok(stateId);
    }

    @ApiModelProperty("审批-博通审-客户信息")
    @PostMapping("/do-approval/bto/customer")
    public R doBtoCustomer(@RequestBody ApprovalReq approvalReq){
        approvalReq.setApprovalType(Constants.APPROVAL_BTO);
        approvalReq.setStageId(BizStageEnum.CUSTOMER_INFO.getStageId());
        approvalReq.setTaskId(null);
        return R.ok(btoOrderStateService.doApproval(approvalReq));
    }


    @ApiModelProperty("提交-初审-踏勘信息")
    @PostMapping("/put-approval/pre/takan")
    public R<String> putPreTaKan(@RequestParam String orderId){
        String stateId = btoOrderStateService.updateToWaitingApprovalState(orderId , BizStageEnum.KAN_CHA.getStageId(),
                BizTaskEnum.TASK_TA_KAN.getTaskId());
        return R.ok(stateId);
    }


    @ApiModelProperty("审批-内审-踏勘信息")
    @PostMapping("/do-approval/pre/takan")
    public R doPreTaKan(@RequestBody ApprovalReq approvalReq){
        approvalReq.setApprovalType(Constants.APPROVAL_INNER);
        approvalReq.setStageId(BizStageEnum.KAN_CHA.getStageId());
        approvalReq.setTaskId(BizTaskEnum.TASK_TA_KAN.getTaskId());
        return R.ok(btoOrderStateService.doApproval(approvalReq));
    }
    @ApiModelProperty("审批-博通审-踏勘信息")
    @PostMapping("/do-approval/bto/takan")
    public R doBtoTaKan(@RequestBody ApprovalReq approvalReq){
        approvalReq.setApprovalType(Constants.APPROVAL_BTO);
        approvalReq.setStageId(BizStageEnum.KAN_CHA.getStageId());
        approvalReq.setTaskId(BizTaskEnum.TASK_TA_KAN.getTaskId());
        return R.ok(btoOrderStateService.doApproval(approvalReq));
    }

    @ApiModelProperty("提交审批-技术勘察")
    @PostMapping("/put-approval/bto/tec-takan")
    public R putPreTecKan(@RequestParam String orderId){
        String stateId = btoOrderStateService.updateToWaitingApprovalState(orderId , BizStageEnum.KAN_CHA.getStageId(),
                BizTaskEnum.TASK_JSKC.getTaskId());
        return R.ok(stateId);
    }

    @ApiModelProperty("审批-博通审-技术勘察")
    @PostMapping("/do-approval/bto/tec-takan")
    public R doTecKan(@RequestBody ApprovalReq approvalReq){
        approvalReq.setApprovalType(Constants.APPROVAL_BTO);
        approvalReq.setStageId(BizStageEnum.KAN_CHA.getStageId());
        approvalReq.setTaskId(BizTaskEnum.TASK_JSKC.getTaskId());
        return R.ok(btoOrderStateService.doApproval(approvalReq));
    }

    @ApiModelProperty("提交审批-设计资料")
    @PostMapping("/put-approval/bto/design")
    public R putDesign(@RequestParam String orderId){
        String currentDesignTaskId = btoDesignService.getCurrentDesignTaskId(orderId);
        String stateId = btoOrderStateService.updateToWaitingApprovalState(orderId , BizStageEnum.DESIGN.getStageId(),
                currentDesignTaskId);
        return R.ok(stateId);
    }

    @ApiModelProperty("审批-博通审-设计资料")
    @PostMapping("/do-approval/bto/design")
    public R doApprovalDesign(@RequestBody ApprovalReq approvalReq){
        String currentDesignTaskId = btoDesignService.getCurrentDesignTaskId(approvalReq.getOrderId());
        approvalReq.setApprovalType(Constants.APPROVAL_BTO);
        approvalReq.setStageId(BizStageEnum.DESIGN.getStageId());
        approvalReq.setTaskId(currentDesignTaskId);
        return R.ok(btoOrderStateService.doApproval(approvalReq));
    }

    @ApiModelProperty("提交审批-施工信息")
    @PostMapping("/put-approval/bto/construction")
    public R putApprovalConstruction(@RequestParam String orderId  , @RequestParam(required = false) String taskId){
        String stateId = btoOrderStateService.updateToWaitingApprovalState(orderId , BizStageEnum.CONSTRUCT.getStageId(),
                taskId);
        return R.ok(stateId);
    }

//    @ApiModelProperty("提交审批-设备信息")
//    @PostMapping("/put-approval/pre/device")
//    public R putApprovalDevice(String orderId){
//        String stateId = btoOrderService.updateToWaitingApprovalState(orderId , BizStageEnum.CONSTRUCT.getStageId(),
//                BizTaskEnum.TASK_SBXX.getTaskId());
//        return R.ok(stateId);
//    }

//    @ApiModelProperty("提交审批-电站投保信息")
//    @PostMapping("/put-approval/pre/ps-insure")
//    public R putApprovalPsInsure(String orderId){
//        String stateId = btoOrderService.updateToWaitingApprovalState(orderId , BizStageEnum.CONSTRUCT.getStageId(),
//                BizTaskEnum.TASK_SGXX.getTaskId());
//        return R.ok(stateId);
//    }

    @ApiModelProperty("提交审批-施工照片信息")
    @PostMapping("/put-approval/bto/construct-images")
    public R putApprovalConstructImages(@RequestParam String orderId){
        String stateId = btoOrderStateService.updateToWaitingApprovalState(orderId , BizStageEnum.CONSTRUCT.getStageId(),
                BizTaskEnum.TASK_SGYXJXX.getTaskId());
        return R.ok(stateId);
    }

    @ApiModelProperty("提交审批-备案信息")
    @PostMapping("/put-approval/bto/record")
    public R putApprovalRecord(@RequestParam String orderId){
        String stateId = btoOrderStateService.updateToWaitingApprovalState(orderId , BizStageEnum.BAZING.getStageId(),
                BizTaskEnum.TASK_BAZXX.getTaskId());
        return R.ok(stateId);
    }

    @ApiModelProperty("审批-博通审-备案信息")
    @PostMapping("/do-approval/bto/record")
    public R doApprovalRecord(@RequestBody ApprovalReq approvalReq){
        approvalReq.setApprovalType(Constants.APPROVAL_BTO);
        approvalReq.setStageId(BizStageEnum.BAZING.getStageId());
        approvalReq.setTaskId(BizTaskEnum.TASK_BAZXX.getTaskId());
        return R.ok(btoOrderStateService.doApproval(approvalReq));
    }


    @ApiModelProperty("审批-博通审-施工审核")
    @PostMapping("/do-approval/bto/construction")
    public R doApprovalConstruction(@RequestBody ApprovalReq approvalReq){
        approvalReq.setApprovalType(Constants.APPROVAL_BTO);
        approvalReq.setStageId(BizStageEnum.CONSTRUCT.getStageId());
        return R.ok(btoOrderStateService.doApproval(approvalReq));
    }

    @ApiModelProperty("提交审批-并网信息")
    @PostMapping("/put-approval/lease/grid")
    public R putApprovalGrid(@RequestParam String orderId){
        String stateId = btoOrderStateService.updateToWaitingApprovalState(orderId , BizStageEnum.GRID_CONNECTION.getStageId(),
                BizTaskEnum.TASK_BWXX.getTaskId());
        return R.ok(stateId);
    }


    @ApiModelProperty("提交审批-购售电合同")
    @PostMapping("/put-approval/bto/gs-elec-contract")
    public R putApprovalGSContract(@RequestParam String orderId){
        String stateId = btoOrderStateService.updateToWaitingApprovalState(orderId , BizStageEnum.GS_CONTRACT.getStageId(),
                BizTaskEnum.TASK_GSDHTXX.getTaskId());
        return R.ok(stateId);
    }

    @ApiModelProperty("审批-博通审-购售电合同")
    @PostMapping("/do-approval/bto/gs-elec-contract")
    public R doApprovalGSContract(@RequestBody ApprovalReq approvalReq){
        approvalReq.setApprovalType(Constants.APPROVAL_BTO);
        approvalReq.setStageId(BizStageEnum.GS_CONTRACT.getStageId());
        return R.ok(btoOrderStateService.doApproval(approvalReq));
    }

    /**
    *结算类型（1、资方结算，2：供应商并网结算，3：供应商资料结算）
    */
    @ApiModelProperty("提交审批-结算信息")
    @PostMapping("/put-approval/bto/settle")
    public R putApprovalGridSettle(@RequestParam String orderId ,@RequestParam Byte setType  ){
        String taskId = null;
        if(DBConstants.SETTL_BW.equals(setType)){
            taskId =  BizTaskEnum.TASK_BWJSXX.getTaskId();
        }
        else if(DBConstants.SETTL_ZL.equals(setType)){
            taskId =  BizTaskEnum.TASK_ZLJSXX.getTaskId();
        }
        String stateId = btoOrderStateService.updateToWaitingApprovalState(orderId , BizStageEnum.SETTLEMENT.getStageId(),
                taskId);
        return R.ok(stateId);
    }

    @ApiModelProperty("审批-博通审-结算信息")
    @PostMapping("/do-approval/bto/settle")
    public R doBtoApprovalGridSettle(@RequestBody ApprovalReq approvalReq){
        Byte setType = approvalReq.getSetType();
        String taskId = null;
        if(DBConstants.SETTL_BW.equals(setType)){
            taskId =  BizTaskEnum.TASK_BWJSXX.getTaskId();
        }
        else if(DBConstants.SETTL_ZL.equals(setType)){
            taskId =  BizTaskEnum.TASK_ZLJSXX.getTaskId();
        }

        approvalReq.setApprovalType(Constants.APPROVAL_BTO);
        approvalReq.setStageId(BizStageEnum.SETTLEMENT.getStageId());
        approvalReq.setTaskId(taskId);


        return R.ok(btoOrderStateService.doApproval(approvalReq));
    }


//    @ApiModelProperty("提交审批-资料结算信息")
//    @PostMapping("/put-approval/pre/grid-means")
//    public R putApprovalGridMeans(String orderId){
//        String stateId = btoOrderStateService.updateToWaitingApprovalState(orderId , BizStageEnum.GRID_CONNECTION.getStageId(),
//                BizTaskEnum.TASK_ZLJSXX.getTaskId());
//        return R.ok(stateId);
//    }

//    @ApiModelProperty("审批-博通审-并网资料信息")
//    @PostMapping("/do-approval/pre/grid-means")
//    public R doBtoApprovalGridMeans(ApprovalReq approvalReq){
//        return R.ok(btoOrderStateService.doApproval(approvalReq));
//    }




    /**
    * 越秀回调
    */
    @ApiModelProperty("越秀主动推送同步订单状态")
    @PostMapping("/sync-approval/lease/status")
    public LeaseApprovaledNotifyResp syncApprovalLeaseStatus(@RequestBody LeaseApprovalNotifyReq leaseApprovaledNotifyReq){
        log.info(" ===================越秀回调start====================== ");
        return leaseApi.syncApprovalLeaseStatus(leaseApprovaledNotifyReq);
    }


    /**
    *  越秀状态查询
    */
    @ApiModelProperty("被动查询单据来同步订单状态")
    @PostMapping("/query/update-approval/lease/status")
    public R<String> updateLeaseStateToBtoState(@RequestParam String orderId) {
        leaseApi.updateLeaseStateToBtoState(orderId);
        return R.ok(null,"状态同步成功！");
    }

    @ApiModelProperty("查询进件后越秀单据状态")
    @GetMapping("/query/approval/lease/status")
    public R<OperatingStatusQueryRes> getLeaseState(@RequestParam String orderId) {
        return R.ok(leaseApi.operatingStatusQuery(orderId));
    }

    @ApiModelProperty("经营租赁资料与合同推送  - 异步")
    @PostMapping("/async/upload/file")
    public R<String> asyncUploadFile(@RequestParam String orderId, @RequestParam List<String> types, @RequestParam boolean isLeaseSyncProject) {
        JSONObject jsonObject = leaseApi.fileAndContactAsyncPush(orderId, types, isLeaseSyncProject);
        String type = (String) jsonObject.get("type");
        if ("P".equals(type)) {
            return R.ok("处理中(附件下载地址接收成功，异步下载");
        } else if ("S".equals(type)) {
            return R.ok("成功！");
        } else {
            return R.fail("失败！");
        }
    }

    /**
     * 附件异步结果查询
     */
    @ApiModelProperty("附件异步结果查询")
    @GetMapping("/get-async-upload-file/query/result")
    public FileAndContactAsyncPushRes getFileAndContactAsyncPush(@RequestParam String queryId){
        return leaseApi.fileAndContactAsyncPushResult(queryId);
    }


    @ApiModelProperty("审批-测试")
    @PostMapping("/do-approval/test")
    public R doApprovalTest(@RequestBody ApprovalReq approvalReq){
        return R.ok(btoOrderStateService.doApproval(approvalReq));
    }

}
